package com.googlecode.gwtbb.client.only;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.i18n.client.DateTimeFormat;

import java.util.Date;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Apr 30, 2008
 */
public class GwtUtils {
	public static DateTimeFormat dateFormatDay = DateTimeFormat.getFormat("yyyy/MM/dd");
	public static DateTimeFormat dateFormatSec = DateTimeFormat.getFormat("yyyy/MM/dd HH:mm:ss");
	// this is an ugly way to do this
	public static String deployPath = "gwtbb";


	public static native void click(Element element) /*-{
		element.click();
	}-*/;

	public static void click(Widget widget) {
		click(widget.getElement());
	}

	public static native String getUserAgent() /*-{
		return navigator.userAgent.toLowerCase();
	}-*/;

	public static HasHorizontalAlignment.HorizontalAlignmentConstant getHorizontalAlignment(HTMLTable.CellFormatter cellFormatter, int row, int column) {
		HasHorizontalAlignment.HorizontalAlignmentConstant retVal = null;
		String align = DOM.getElementProperty(cellFormatter.getElement(row, column), "align");
		if ("left".equals(align))
			retVal = HasHorizontalAlignment.ALIGN_LEFT;
		else if ("center".equals(align))
			retVal = HasHorizontalAlignment.ALIGN_CENTER;
		else if ("right".equals(align))
			retVal = HasHorizontalAlignment.ALIGN_RIGHT;
		return retVal;
	}

	public static HasVerticalAlignment.VerticalAlignmentConstant getVerticalAlignment(HTMLTable.CellFormatter cellFormatter, int row, int column) {
		HasVerticalAlignment.VerticalAlignmentConstant retVal = null;
		String align = DOM.getElementProperty(cellFormatter.getElement(row, column), "verticalAlign");
		if ("top".equals(align))
			retVal = HasVerticalAlignment.ALIGN_TOP;
		else if ("middle".equals(align))
			retVal = HasVerticalAlignment.ALIGN_MIDDLE;
		else if ("bottom".equals(align))
			retVal = HasVerticalAlignment.ALIGN_BOTTOM;
		return retVal;
	}

	public static String formatDay(Date date) {
		return dateFormatDay.format(date);
	}

	public static String formatSec(Date date) {
		return dateFormatSec.format(date);
	}

	public static ServiceDefTarget createProxy(Object proxy, String servicePath) {
		ServiceDefTarget retVal = (ServiceDefTarget)proxy;
		// maybe the class-literal limitation will go away in future versions
		//if (serviceClass == CategoryService.class)
		//	retVal = (ServiceDefTarget)GWT.create(CategoryService.class);
		//else
		//throw new IllegalArgumentException("invalid serviceClass: " + serviceClass);

		retVal.setServiceEntryPoint(getServiceUrl(servicePath));
		return retVal;
		//return retVal;
	}

	public static String getServiceUrl(String servicePath) {
		String hostPageBaseUrl = GWT.getHostPageBaseURL();
		int index = 0;
		for (int i = 0; i < 3; i++)
			index = hostPageBaseUrl.indexOf('/', index + 1);
		return hostPageBaseUrl.substring(0, index + 1) + deployPath + servicePath;
	}

	public static String getRealUrl(String url) {
		return GWT.getModuleBaseURL() + url;
	}

	public static void handleFailure(Throwable throwable) {
		// todo display server error properly
		Window.alert(throwable.getMessage());
	}

	private GwtUtils() {
	}
}
